#include <ctype.h>
#include <stdarg.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "cms_common.h"
#include "libmdm.h"
#include "cms_msg.h"
#include "cms_mdm.h"
#include "cmsXml_object.h"
#include "cms_utils.h"
#include "cms_event.h"
#include "cms_log.h"

/*======================================================================================================*/

char *getPreDefaultXml(void)
{
	char countryBuf[32] = {0};
	char provinceBuf[32] = {0};
	static char preDefaultXml[96] = {0};

    if(strlen(preDefaultXml)){
        return preDefaultXml;
    }
    memset(preDefaultXml, 0, sizeof(preDefaultXml));
	
#if 0
	libgc_sys_getArea(countryBuf);
	libgc_sys_getProvince(provinceBuf);
#endif
	CMS_LOG(LL_PRINT|LL_SYSLOG, "currrent onu version is for area: %s, province %s.", countryBuf, provinceBuf);
	if(strlen(countryBuf) == 0 ||strlen(provinceBuf) == 0){
		CMS_LOG(LL_PRINT, "countryarea or province info is not set!!! now load default xml not by country&province Info!!!");
	}else{
	    snprintf(preDefaultXml, sizeof(preDefaultXml) - 1, "/usr/config/%s_%s.default.enc", countryBuf, provinceBuf);
    }

	if(strlen(preDefaultXml) > 0){
		if(access(preDefaultXml, F_OK) == -1){
			CMS_LOG(LL_ERROR|LL_SYSLOG, "default xml (%s) is not found!!!", preDefaultXml);
			snprintf(preDefaultXml, sizeof(preDefaultXml) - 1, CMS_DEF_FILE_ENC);
		}
	}else{
		snprintf(preDefaultXml, sizeof(preDefaultXml) - 1, CMS_DEF_FILE_ENC);
	}
	
	CMS_LOG(LL_PRINT, "final default xml is %s.", preDefaultXml);
	
	return preDefaultXml;
}

/*======================================================================================================*/

int cms_is_hex_string(char *input_str)
{
	int len;
	int i;

	len = strlen(input_str);
	for (i = 0; i < len; i++) {
		if (isxdigit(input_str[i]) == 0)
			return 0;
		else
			continue;
	}

	return 1;
}

void cmsReplace(char *s, char what, char with)
{
	while (*s) {
		if (what == *s)
			*s = with;
		++s;
	}
}

int generateName(uint32_t wanIndex,char *ServiceList, char *ConnectionType,uint16_t VLANID, uint8_t VLANMode, char *wanpon_connectname)
{
	char tempBuf[47]={0};
#ifdef GC_APPS_CTC
	if(strstr(ServiceList,"VOIP")!=NULL){
		strncpy(tempBuf,ServiceList,sizeof(tempBuf));
		str_replace(tempBuf, sizeof(tempBuf), "VOIP", "VOICE");
	}
	else
#endif
	strncpy(tempBuf,ServiceList,sizeof(tempBuf));
	if(strstr(tempBuf,",")){
		cmsReplace(tempBuf,',','_');
	}
	if(VLANMode == 0){
		sprintf(wanpon_connectname,"%d_%s_%s_VID_",wanIndex,tempBuf,(strstr(ConnectionType,"Routed")!= NULL?"R":"B"));
	}else if(VLANMode == 1){
		sprintf(wanpon_connectname,"%d_%s_%s_VID_0",wanIndex,tempBuf,(strstr(ConnectionType,"Routed")!= NULL?"R":"B")); 
	}else if(VLANMode == 2){
		sprintf(wanpon_connectname,"%d_%s_%s_VID_%d",wanIndex,tempBuf,(strstr(ConnectionType,"Routed")!= NULL?"R":"B"),VLANID); 
	}

	return 0;
}

unsigned int Cms_getpvcIndex(cmsXmlCb_t *xmlShmCb){
	int WANDevMax = 0,WANConDevMax = 0,WANIPMax = 0, WANPPPMax = 0;
	int iWANDev = 0,iWANCDev = 0,iIPEntry = 0,iPPPEntry = 0;
	char objName[256]={0};
	void *object = NULL;
	uint32_t wanIndex = 0,maxIndex = 0;
	cmsObj_InternetGatewayDevice_WANDevice_i_WANConnectionDevice_i_WANIPConnection_i_t *WANIPCon_p = NULL;
	cmsObj_InternetGatewayDevice_WANDevice_i_WANConnectionDevice_i_WANPPPConnection_i_t *WANPPPCon_p = NULL;
	WANDevMax = InternetGatewayDevice_WANDevice_i_Max;
	WANConDevMax = InternetGatewayDevice_WANDevice_i_WANConnectionDevice_i_Max;
	WANPPPMax = InternetGatewayDevice_WANDevice_i_WANConnectionDevice_i_WANPPPConnection_i_Max;
	WANIPMax = InternetGatewayDevice_WANDevice_i_WANConnectionDevice_i_WANIPConnection_i_Max;
	
	for(iWANDev = 1; iWANDev <= WANDevMax; iWANDev++)
	{
		sprintf(objName, "InternetGatewayDevice.WANDevice.%d", iWANDev);
		object = cmsXml_GetObjectByName(xmlShmCb, objName, NULL_IF_NOT_EXISTED);
		if(object == NULL){
			continue;
		}	
		
		for(iWANCDev = 1; iWANCDev <= WANConDevMax; iWANCDev++)
		{
			sprintf(objName, "InternetGatewayDevice.WANDevice.%d.WANConnectionDevice.%d", iWANDev, iWANCDev);
			object = cmsXml_GetObjectByName(xmlShmCb, objName, NULL_IF_NOT_EXISTED);
			if(object == NULL){
				continue;
			}	
		
			for(iPPPEntry = 1; iPPPEntry <= WANPPPMax; iPPPEntry++)
			{
				sprintf(objName, "InternetGatewayDevice.WANDevice.%d.WANConnectionDevice.%d.WANPPPConnection.%d", iWANDev, iWANCDev, iPPPEntry); 
				object = cmsXml_GetObjectByName(xmlShmCb, objName, NULL_IF_NOT_EXISTED);
				if(object != NULL){
					WANPPPCon_p = (cmsObj_InternetGatewayDevice_WANDevice_i_WANConnectionDevice_i_WANPPPConnection_i_t *)object;
					if(WANPPPCon_p->wanIndex != 0 ){
						wanIndex = WANPPPCon_p->wanIndex;
					}
				}
				if(wanIndex > maxIndex){
					maxIndex = wanIndex;
				}
			}
			
			for(iIPEntry = 1; iIPEntry <= WANIPMax; iIPEntry++)
			{
				sprintf(objName, "InternetGatewayDevice.WANDevice.%d.WANConnectionDevice.%d.WANIPConnection.%d", iWANDev, iWANCDev, iIPEntry); 
				object = cmsXml_GetObjectByName(xmlShmCb, objName, NULL_IF_NOT_EXISTED);
				if(object != NULL){
					WANIPCon_p = (cmsObj_InternetGatewayDevice_WANDevice_i_WANConnectionDevice_i_WANIPConnection_i_t *)object;
					if(WANIPCon_p->wanIndex != 0){
						wanIndex = WANIPCon_p->wanIndex;
					}
				}
				if(wanIndex > maxIndex){
					maxIndex = wanIndex;
				}
			}
		}
	}
	
	return maxIndex + 1;

}


